package pe.gastobien.app.layer.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import pe.gastobien.app.domain.Category;
import pe.gastobien.app.domain.User;
import pe.gastobien.app.layer.service.CategoryService;

/**
 * Servlet implementation class CategoryController
 */
@WebServlet({ "/CategoryController", "/getCategoryById", "/addNewCategory","/updateCategory", "/deleteCategory", "/showCategories" })
public class CategoryController extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String alias = request.getServletPath();
		if(alias.equals("/getCategoryById")){
			getCategoryById(request,response);
		}
		if(alias.equals("/addNewCategory"))
		{
			addNewCategory(request, response);
		}
		if(alias.equals("/updateCategory"))
		{
			updateCategory(request,response);
		}
		if(alias.equals("/deleteCategory"))
		{
			deleteCategory(request,response);
		}
	}
	
	private void showCategories(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String url = "";
		try
		{
			CategoryService service = new CategoryService();		
			HttpSession session = request.getSession(true);
			User user = (User)session.getAttribute("user");
			List<Category> categories = service.getCategories(user.getUserID(),2013,8);
			request.setAttribute("categories", categories);
			url = "categoryItems.jsp";
		}catch(Exception e)
		{
			request.setAttribute("error", e.getMessage());
			url = "login.jsp";
		}	
		
		RequestDispatcher rd = request.getRequestDispatcher(url);
		rd.forward(request, response);
	}

	private void deleteCategory(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		CategoryService service = new CategoryService();
		Integer categoryId = Integer.parseInt(request.getParameter("categoryId"));
		service.delete(categoryId);
		RequestDispatcher rd = request.getRequestDispatcher("categoryItems.jsp");
		rd.forward(request, response);
	}

	private void updateCategory(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		Integer categoryId = Integer.parseInt(request.getParameter("categoryId"));
		String categoryName = request.getParameter("categoryName");
		String categoryDescription = request.getParameter("categoryDescription");
		CategoryService service = new CategoryService();
		Category category = new Category();		
		category.setCategoryID(categoryId);		
		category.setCategoryName(categoryName);
		category.setCategoryDescription(categoryDescription);
		service.update(category);
		RequestDispatcher rd = request.getRequestDispatcher("categoryItems.jsp");
		rd.forward(request, response);
	}

	private void addNewCategory(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		HttpSession session = request.getSession(true);
		CategoryService service = new CategoryService();
		Category category = new Category();
		String categoryName = request.getParameter("categoryName");
		String categoryDescription = request.getParameter("categoryDescription");
		User user = (User)session.getAttribute("user");
		category.setUserID(user.getUserID());
		category.setActive(true);
		category.setCategoryType(1);
		category.setCategoryName(categoryName);
		category.setCategoryDescription(categoryDescription);
		//category.setYearBudget(DateFormat.YEAR_FIELD);
		//category.setMonthBudget(DateFormat.MONTH_FIELD);
		category.setMonthBudget(8);
		category.setYearBudget(2013);
		service.save(category);
		RequestDispatcher rd = request.getRequestDispatcher("categoryItems.jsp");
		rd.forward(request, response);		
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String alias = request.getServletPath();
		if(alias.equals("/getCategoryById")){
			getCategoryById(request,response);
		}
		if(alias.equals("/showCategories"))
		{
			showCategories(request,response);
		}
	}
	
	protected void getCategoryById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		CategoryService service = new CategoryService();
		Integer categoryId = Integer.parseInt(request.getParameter("categoryId"));
		Category category = service.getCategoryById(categoryId);
		request.setAttribute("category", category);
		RequestDispatcher rd = request.getRequestDispatcher("category.jsp");
		rd.forward(request, response);
	}
}
